#!/bin/tcsh -ef
#############################################################################
#                                                                           #
# Title: Analyze pre-refinement results         			    #
#                                                                           #
# (C) 2dx.org, GNU Plublic License.                                         #
#                                                                           #
# Created..........: 29/07/2016                                             #
# Last Modification: 29/07/2016                                             #
# Author...........: 2dx.org                                                #
#                                                                           #
#############################################################################
#
# SORTORDER: 6
#
#
# MANUAL: This script will generate plots showing how much the alignment parameters (Euler angles and shifts) have changed during the pre-refinement.

# MANUAL: For a good dataset that has been properly processed with 2dx, the parameters for all or most of the images should change very little.
#
# DISPLAY: SPR_DIR
# DISPLAY: SPR_WhichStack
# DISPLAY: SPR_PREREFINE_DIR
# DISPLAY: SPR_FRLPreRefAnalyzeCycleIni
# DISPLAY: SPR_FRLPreRefAnalyzeCycleFin
#
#$end_local_vars
#
set bin_2dx = ""
set proc_2dx = ""

set scriptname = AnalyzePreRefineResults

#
set SPR_DIR = ""
set SPR_WhichStack = ""
set SPR_PREREFINE_DIR = ""
set SPR_FRLPreRefAnalyzeCycleIni = ""
set SPR_FRLPreRefAnalyzeCycleFin = ""
#
set SPR_FREALIGN_DIR = ${SPR_DIR}/FREALIGN/
set SPR_PREREFINE_DIR = ${SPR_FREALIGN_DIR}/${SPR_PREREFINE_DIR}/

#$end_vars

set ccp4_setup = 'y'
source ${proc_2dx}/initialize
#
if ( -e LOGS/${scriptname}.results ) then
	mv LOGS/${scriptname}.results LOGS/${scriptname}.results.old
endif

${app_python} ${proc_2dx}/SPR_FrealignComparePar.py ${SPR_PREREFINE_DIR}/particles_${SPR_WhichStack}_crystal-avg_${SPR_FRLPreRefAnalyzeCycleIni}_r1.par ${SPR_PREREFINE_DIR}/particles_${SPR_WhichStack}_crystal-avg_${SPR_FRLPreRefAnalyzeCycleFin}_r1.par  ${SPR_PREREFINE_DIR}/particles_${SPR_WhichStack}_crystal-avg_${SPR_FRLPreRefAnalyzeCycleIni}-${SPR_FRLPreRefAnalyzeCycleFin}_diff.txt ${SPR_PREREFINE_DIR}

echo "# IMAGE: ${SPR_PREREFINE_DIR}/TLTAXIS_change_hist.png <PNG: TLTAXIS_change_hist.png>" >> LOGS/${scriptname}.results
echo "# IMAGE: ${SPR_PREREFINE_DIR}/TLTANG_change_hist.png <PNG: TLTANG_change_hist.png>" >> LOGS/${scriptname}.results
echo "# IMAGE: ${SPR_PREREFINE_DIR}/TAXA_change_hist.png <PNG: TAXA_change_hist.png>" >> LOGS/${scriptname}.results
echo "# IMAGE: ${SPR_PREREFINE_DIR}/SHX_change_hist.png <PNG: SHX_change_hist.png>" >> LOGS/${scriptname}.results
echo "# IMAGE: ${SPR_PREREFINE_DIR}/SHY_change_hist.png <PNG: SHY_change_hist.png>" >> LOGS/${scriptname}.results
echo "# IMAGE-IMPORTANT: ${SPR_PREREFINE_DIR}/angular_change_hist.png <PNG: angular_change_hist.png>" >> LOGS/${scriptname}.results
echo "# IMAGE-IMPORTANT: ${SPR_PREREFINE_DIR}/translational_change_hist.png <PNG: translational_change_hist.png>" >> LOGS/${scriptname}.results

foreach i (${SPR_PREREFINE_DIR}/particles_${SPR_WhichStack}*.mrc)
	echo "# IMAGE: $i <MAP: `basename $i`>" >> LOGS/${scriptname}.results
end

echo ":: "
echo ":: Done!"
echo ":: "
